package com.ys.tdm.dao.doc.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.ys.tdm.bean.doc.Doc;
import com.ys.tdm.dao.doc.IDocDao;
import com.ys.tdm.templ.MybatisTemplate;
import com.ys.tdm.util.StringTools;
import com.ys.tdm.util.UUIDGenerator;




/**
 * 模块信息Dao实现
 * @author Troy
 * @date 2018-01-02 09:25:58
 */
@Repository
public class DocDaoImpl extends MybatisTemplate implements IDocDao {

	@Override
	public Doc getDocById(String id) throws Exception {
		return (Doc)this.selectOne("DocXML.getDocById", id);
	}
	
	@Override
	public List<Doc> getDocByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		return this.selectList("DocXML.getDocByIds", params);
	}
	
	@Override
	public List<Doc> getDocByIdsList(List<String> ids) throws Exception {
		return this.selectList("DocXML.getDocByIdsList", ids);
	}

	@Override
	public List<Doc> getAll(Doc doc) throws Exception {
		return this.selectList("DocXML.getAll", doc);
	}
	
	@Override
	public int getByPageCount(Doc doc)throws Exception {
		return this.selectOne("DocXML.getByPageCount", doc);
	}

	@Override
	public void insertDoc(Doc doc) throws Exception {
		if (null != doc) {
			doc.setCreateTime(new Date());
			doc.setUpdateTime(new Date());
			this.insert("DocXML.insertDoc", doc);
		}
	}
	
	@Override
	public void insertDocBatch(List<Doc> docs) throws Exception {
		if(CollectionUtils.isNotEmpty(docs)){
			for (Doc doc : docs) {
				if (null != doc) {
					doc.setId(UUIDGenerator.generate());
					doc.setCreateTime(new Date());
				}
			}
			this.insert("DocXML.insertDocBatch", docs);
		}
	}
	
	@Override
	public void delDocById(String id) throws Exception {
		this.delete("DocXML.delDocById", id);
	}
	
	@Override
	public void delDocByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		this.delete("DocXML.delDocByIds", params);
	}
	
	@Override
	public void delDocByIdsList(List<String> ids) throws Exception {
		this.delete("DocXML.delDocByIdsList", ids);
	}
	
	@Override
	public int updateDoc(Doc doc) throws Exception {
		if (null != doc) {
			doc.setUpdateTime(new Date());
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(doc);
			return this.update("DocXML.updateDoc", doc);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateDocByIds(String ids,Doc doc) throws Exception {
		ids = StringTools.converString(ids);
		if (StringUtils.isNotBlank(ids) && null != doc) {
			doc.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(doc);
			params.put("doc", doc);
			return this.update("DocXML.updateDocByIds", params);
		} else {
			return 0;
		}
		
	}
	
	@Override
	public int updateDocByIdsList(List<String> ids,Doc doc) throws Exception {
		if (CollectionUtils.isNotEmpty(ids) && null != doc) {
			doc.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(doc);
			params.put("doc", doc);
			return this.update("DocXML.updateDocByIdsList", params);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateDocList(List<Doc> docs) throws Exception {
		if(CollectionUtils.isNotEmpty(docs)){
			for (Doc doc : docs) {
				if (null != doc) {
					doc.setUpdateTime(new Date());
					/**清理不需要更新的数据*/
					this.cleanWhenUpdate(doc);
				}
			}
			return this.update("DocXML.updateDocList", docs);
		} else {
			return 0;
		}
	}
	
	/**
	 * 清理不需要更新的数据
	 * @param orderRebok
	 * @Description:
	 * @author wentaoxiang 2016年6月1日 下午5:19:16
	 */
	private void cleanWhenUpdate(Doc doc) {
		doc.setCreateTime(null);
		doc.setCreator(null);
	}
	
	//------------api------------
}

